#!/bin/sh

#
#  jira-testenvmaker
#
#  Copies everything you need - or at least what I need - to make a
#  JIRA test environment with a recent copy of production data.
#
#  We assume you are running:
#  * A reasonably modern Linux
#  * production jira at a machine "jira"
#  * this script on a machine "jiratest"
#  * Tomcat
#  * A WAR/EAR (not standalone) version of JIRA...
#  * ...stored in a Subversion repository at https://subversion
#
#  This is provided as a starting point for folks to copy and is definitely
#  not drop-in software; my goal is to save you some time. Items marked !!! 
#  will certainly need to be changed to reflect your environment. 
#
#  Copyright 2011 Chris Brown, Backstop Solutions Group
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

umask=077
export UMASK=077

export JAVA_HOME=!!! something like /usr/local/java
export JAVA_OPTS='-Xmx4000m -XX:MaxPermSize=256m'
JIRAHOME=!!! where jira.home lives on the *test* machine, like /home/jira
SOURCES="${JIRAHOME}/src/jira"
WEBAPPS=!!! where the built app should be deployed, like /home/jira/apache-tomcat-5.5.30/webapps
WARNAME=!!! the name of the war; likely ROOT.war for tomcat
WARFULL=${WEBAPPS}/${WARNAME}

echo "Shutting down Tomcat"
pkill -9 java

echo "Getting a copy of jira.home from production"
rm /home/jira/jira-home.tar.gz
#
# "pushhome" on the jira production side just copies JIRA home to the test machine:
#    cd /var/lib/tomcat5/jira_stuff/; tar -pczf /tmp/jira-home.tar.gz jira.home
#    scp /tmp/jira-home.tar.gz jira@jiratest:jira-home.tar.gz                  
#
ssh jira@jira pushhome
rm -rf /home/jira/jira.home
cd /home/jira

echo "Extracting jira.home"
tar xzvf /home/jira/jira-home.tar.gz > /dev/null
rm /home/jira/jira.home/.jira-home.lock

echo "Copying production DB"
/usr/bin/mysqldump -h !!! -u !!! --password=!!! --skip-lock-tables !!! > /tmp/jiradb4.sql

echo "Loading to jiratest DB"
/usr/bin/mysql -h !!! -u !!! --password=!!! -e "drop database if exists !!!"
/usr/bin/mysql -h !!! -u !!! --password=!!! -e "create database !!!"
/usr/bin/mysql -h !!! -D !!! -u !!! --password=!!! < /tmp/jiradb4.sql
/usr/bin/mysql -h !!! -D !!! -u !!! --password=!!! -e "update mailserver set PREFIX='***TEST MESSAGE***', smtp_port=9999 where id=10000" 

echo "Grabbing fresh copy of JIRA from subversion"
[ -d "${SOURCES}" ] && [ "x" != "x${SOURCES}" ] && /bin/rm -rf "${SOURCES}" 
/usr/bin/svn export https://subversion/misc/jira/trunk "${SOURCES}" > /dev/null

echo "Modifying checkout for test environment"
perl -pi -e '$d=`date +%Y-%m-%dT%H:%M:%S`;s/# jira.alertheader =/jira.alertheader =This is JIRA test built on $d/g' ${SOURCES}/edit-webapp/WEB-INF/classes/jira-application.properties
cd "${SOURCES}"
vers=`/bin/grep "<property name=\"version\"" build.xml  | /usr/bin/awk -F= '{print $3}' | /bin/sed -e "s/[^0-9\.b-]\+//g"`

echo "Building JIRA-${vers}"
/bin/sh build.sh

echo "Backing up current version of JIRA"
/bin/cp -a "${WARFULL}" "${JIRAHOME}/${WARNAME}.bak"

echo "Removing old version of JIRA and associated extracted war"
/bin/rm -rf "${WEBAPPS}/ROOT" "${WARFULL}"

echo "Deploying freshly built JIRA-${vers}"
/bin/cp "${SOURCES}/dist-tomcat/atlassian-jira-${vers}.war" "${WARFULL}"

echo "Exporting plugins"
/bin/rm "${JIRAHOME}/jira.home/plugins/installed-plugins/*"
/usr/bin/svn export --force https://subversion/svn/trunk/tools/JiraPlugin/installed-plugins "${JIRAHOME}/jira.home/plugins/installed-plugins/"
#
echo "Starting Tomcat"
"${WEBAPPS}/../bin/startup.sh"
